﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebDAV_Test_Application
{
    public partial class FormHeaders : Form
    {
        public FormHeaders()
        {
            InitializeComponent();
        }

        public List<string[]> EditHeaders(List<string[]> Headers, IWin32Window Parent)
        {
            // Edit the headers given

            // Show the headers in the datagridview
            dataGridViewHeaders.Rows.Clear();
            foreach (string[] sHeader in Headers)
            {
                DataGridViewRow oRow = new DataGridViewRow();
                oRow.Cells.Add(new DataGridViewTextBoxCell());
                oRow.Cells.Add(new DataGridViewTextBoxCell());
                oRow.Cells[0].Value = sHeader[0];
                oRow.Cells[1].Value = sHeader[1];
                dataGridViewHeaders.Rows.Add(oRow);
            }
            this.ShowDialog(Parent);

            // Now read the headers and return them
            List<string[]> oHeaders=new List<string[]>();
            
            for (int i=0; i<dataGridViewHeaders.Rows.Count; i++)
            {
                string[] sHeader = new string[2];
                sHeader[0] = (string)dataGridViewHeaders.Rows[i].Cells[0].Value;
                sHeader[1] = (string)dataGridViewHeaders.Rows[i].Cells[1].Value;
                if (!string.IsNullOrEmpty(sHeader[0]))
                    oHeaders.Add(sHeader);
            }

            return oHeaders;
        }

        public List<string[]> Headers
        {
            get
            {
                // Read headers from the DataGridView and return them as a list
                List<string[]> oHeaders = new List<string[]>();
                for (int i = 0; i < dataGridViewHeaders.Rows.Count; i++)
                {
                    string[] sHeader = new string[2];
                    sHeader[0] = (string)dataGridViewHeaders.Rows[i].Cells[0].Value;
                    sHeader[1] = (string)dataGridViewHeaders.Rows[i].Cells[1].Value;
                    if (!string.IsNullOrEmpty(sHeader[0]))
                        oHeaders.Add(sHeader);
                }
                return oHeaders;
            }

            set
            {
                // Write the passed headers to the DataGridView
                dataGridViewHeaders.Rows.Clear();
                foreach (string[] sHeader in value)
                {
                    DataGridViewRow oRow = new DataGridViewRow();
                    oRow.Cells.Add(new DataGridViewTextBoxCell());
                    oRow.Cells.Add(new DataGridViewTextBoxCell());
                    oRow.Cells[0].Value = sHeader[0];
                    oRow.Cells[1].Value = sHeader[1];
                    dataGridViewHeaders.Rows.Add(oRow);
                }
            }
        }


        private void buttonApply_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
